<template>
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
        <h3>号码登录</h3>
        <el-form-item label="username" prop="username">
            <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="password" prop="password">
            <el-input v-model="ruleForm.password" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
                Create
            </el-button>
        </el-form-item>
    </el-form>
</template>

<script setup>
import { reactive, ref } from 'vue'
import request from '@/request/request'
import router from '@/router'
import { ElMessage } from 'element-plus'
const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: '',
})



const rules = reactive({
    username: [
        { required: true, message: 'Please input Activity name', trigger: 'blur' },
        { pattern:/^1[3-9]\d{9}$/, message: 'Length should be 5 to 8', trigger: 'blur' },
    ],
    password: [
        { required: true, message: 'Please input Activity name', trigger: 'blur' },
        { pattern:/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{6,16}$/, message: 'Length should be 5 to 8', trigger: 'blur' },
    ],
})

const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!', ruleForm.username, ruleForm.password)
            request.post('/login', ruleForm).then(res => {
                console.log(res);
                if(res.data.code==10000){
                    router.push('/home')
                    ElMessage({
                    message: 'Congrats, this is a success message.',
                    type: 'success',
                })
                localStorage.setItem('accessToken', res.data.data.accessToken)
                localStorage.setItem('refreshToken', res.data.data.refreshToken)
                localStorage.setItem('avator', res.data.data.userInfo.avator)
                localStorage.setItem('username', res.data.data.userInfo.username)
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

</script>

<style scoped>
.el-form{
    width: 400px;
    height: 400px;
    border: solid 3px black;
    float: right;
    padding: 20px;
    .el-form-item{
        width: 400px;
        height: 40px;
    }
    .el-button{
        width: 400px;
    }
    h3{
        text-align: center;
    }
}
</style>